
package com.contractmodule.dao;
import com.contractmodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbContractDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbContract tbContract=null;
    private List<TbContract> tbContractList=null;

    public boolean add(TbContract inTbContract){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Contract(ContractCode,ContractName,ContractContent,StartDateSigned,EndDateSigned,PartyAName,PartyAAddress,PartyBName,PartybAddress) values(?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbContract.getContractCode());
        pt.setString(2,inTbContract.getContractName());
        pt.setString(3,inTbContract.getContractContent());
        pt.setString(4,inTbContract.getStartDateSigned());
        pt.setString(5,inTbContract.getEndDateSigned());
        pt.setString(6,inTbContract.getPartyAName());
        pt.setString(7,inTbContract.getPartyAAddress());
        pt.setString(8,inTbContract.getPartyBName());
        pt.setString(9,inTbContract.getPartybAddress());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(String inContractCode){
        return UtilDAO.del("Tb_Contract", "ContractCode", inContractCode);
    }
    public boolean edit(TbContract inTbContract){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Contract set ContractName=?,ContractContent=?,StartDateSigned=?,EndDateSigned=?,PartyAName=?,PartyAAddress=?,PartyBName=?,PartybAddress=? where ContractCode=?";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbContract.getContractName());
        pt.setString(2,inTbContract.getContractContent());
        pt.setString(3,inTbContract.getStartDateSigned());
        pt.setString(4,inTbContract.getEndDateSigned());
        pt.setString(5,inTbContract.getPartyAName());
        pt.setString(6,inTbContract.getPartyAAddress());
        pt.setString(7,inTbContract.getPartyBName());
        pt.setString(8,inTbContract.getPartybAddress());
        pt.setString(9,inTbContract.getContractCode());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbContract getTbContractByContractCode(String inContractCode) {
        tbContract=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract where ContractCode=? ";
            pt = conn.prepareStatement(sql);
            pt.setString(1,inContractCode);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbContract == null)tbContract = getTbContract(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContract;
    }
    public List<TbContract> getTbContractAll() {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractList;
    }
    public List<TbContract> getTbContractAll(VskPage vp) {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Contract", "ContractCode"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbContractList;
    }
    public TbContract getTbContract(ResultSet rs) throws Exception{
        TbContract tbContract = new TbContract();
        tbContract.setContractId(rs.getInt("ContractId"));
        tbContract.setContractCode(rs.getString("ContractCode"));
        tbContract.setContractName(rs.getString("ContractName"));
        tbContract.setContractContent(rs.getString("ContractContent"));
        tbContract.setStartDateSigned(rs.getString("StartDateSigned"));
        tbContract.setEndDateSigned(rs.getString("EndDateSigned"));
        tbContract.setAccessories(rs.getString("Accessories"));
        tbContract.setPartyAName(rs.getString("PartyAName"));
        tbContract.setPartyAAddress(rs.getString("PartyAAddress"));
        tbContract.setPartyAChapter(rs.getString("PartyAChapter"));
        tbContract.setPartyAContact(rs.getString("PartyAContact"));
        tbContract.setPartyAAgent(rs.getString("PartyAAgent"));
        tbContract.setPartyALegalRepresentative(rs.getString("PartyALegalRepresentative"));
        tbContract.setPartyBName(rs.getString("PartyBName"));
        tbContract.setPartybAddress(rs.getString("PartybAddress"));
        tbContract.setPartybChapter(rs.getString("PartybChapter"));
        tbContract.setPartybContact(rs.getString("PartybContact"));
        tbContract.setPartybAgent(rs.getString("PartybAgent"));
        tbContract.setPartybLegalRepresentative(rs.getString("PartybLegalRepresentative"));
        tbContract.setContractAddressSigned(rs.getString("ContractAddressSigned"));
        tbContract.setCreateTime(rs.getString("CreateTime"));
        return tbContract;
    }
}
